#include "stdafx.h"
#include <sstream>
#include "MyControls/ProgressDlg.h"
#include "MyControls/AsyncCall.h"
#include "MyControls/Resource.h"

namespace My {

BEGIN_MESSAGE_MAP(ProgressDlg, CDialog)
	ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()

ProgressDlg::ProgressDlg()
{
	m_Canceled = false;
}

ProgressDlg::ProgressDlg(CWnd * Parent, bool DisableParent)
{
	m_Canceled = false;
	Create(Parent, DisableParent);
}

ProgressDlg::~ProgressDlg()
{
	if (m_hWnd) {
		if (m_DisableParent)
			GetParent()->EnableWindow();
		VERIFY(DestroyWindow());
	}
}

void ProgressDlg::Create(CWnd * Parent, bool DisableParent)
{
	if (m_DisableParent = DisableParent)
		Parent->EnableWindow(FALSE);

	VERIFY(CDialog::Create(IDD_PROGRESS, Parent));
}

void ProgressDlg::Reset(unsigned Max, unsigned Current)
{
	m_Canceled = false;
	m_Curr = Current;
	m_Max = Max;
	ProgressCtrl()->SetRange32(0, m_Max);
	UpdateControls();

	//CenterWindow();
	EnableWindow();
	ShowWindow(SW_SHOW);
}

bool ProgressDlg::Increment()
{
	++m_Curr;
	UpdateControls();
	return !m_Canceled;
}

void ProgressDlg::SetCurrent(unsigned Val)
{
	m_Curr = Val;
	UpdateControls();
}

inline CProgressCtrl * ProgressDlg::ProgressCtrl()
	{ return (CProgressCtrl*)GetDlgItem(IDC_PROGRESS); }

void ProgressDlg::UpdateControls() {
	std::ostringstream stream;
	stream << m_Curr << " / " << m_Max;
	SetDlgItemText(IDC_ACTIVITY, stream.str().c_str());
	ProgressCtrl()->SetPos(m_Curr);
}

void ProgressDlg::OnCancel()
{
	///do not close dialog immediately
	m_Canceled = true;
}

} //namespace My
